package br.com.jw.dao;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import br.com.jw.modelo.Permissoes;
import android.content.Context;

public class PermissoesDao {
	 Context context;
	 String [] result;
	 boolean aux = false;
		public PermissoesDao(Context context) {
			this.context=context;
		}
	
		public boolean verificarConcederPermissao(int per_usu_id, int per_men_id){
			 try{
				   Socket socket = new Socket("10.0.2.2", 7777);
					DataInputStream in = new DataInputStream(socket
							.getInputStream());
					DataOutputStream out = new DataOutputStream(socket
							.getOutputStream());
		            out.writeUTF("permissoes");
					out.writeUTF("verificarPermissao");
					out.writeInt(per_usu_id);
					out.writeInt(per_men_id);
					
					out.flush();
					
					aux = in.readBoolean();
					return aux;
					
			   }catch (Exception e) {
					e.printStackTrace();
					// mostra os erros no log
				e.getMessage();
			}	
				   return aux;
			   }
		
		/*public boolean concederPermissoes(int per_men_id, int usu_id){
			   try{
			   Socket socket = new Socket("10.0.2.2", 7777);
				DataInputStream in = new DataInputStream(socket
						.getInputStream());
				DataOutputStream out = new DataOutputStream(socket
						.getOutputStream());
	            out.writeUTF("permissoes");
				out.writeUTF("concederPermissoes");
				out.writeInt(per_men_id);
				out.writeInt(usu_id);
				out.flush();
				
				aux = in.readBoolean();
				return aux;
				
		   }catch (Exception e) {
				e.printStackTrace();
				// mostra os erros no log
			e.getMessage();
		}	
			   return aux;
		   }*/
		   public boolean excluirPermissoesUsuario(int usu_id, int per_men_id){
			   try{
			   Socket socket = new Socket("10.0.2.2", 7777);
				DataInputStream in = new DataInputStream(socket
						.getInputStream());
				DataOutputStream out = new DataOutputStream(socket
						.getOutputStream());
	            out.writeUTF("permissoes");
				out.writeUTF("excluirPermissoesUsuario");
				out.writeInt(usu_id);
				out.writeInt(per_men_id);
				out.flush();
				
				aux = in.readBoolean();
				return aux;
				
		   }catch (Exception e) {
				e.printStackTrace();
				// mostra os erros no log
			e.getMessage();
		}	
			   return aux;
		   }
           public ArrayList<Permissoes> getPermissoesUsuario(int usu_id){
			
			try {
				Socket socket = new Socket("10.0.2.2", 7777);
				DataInputStream in = new DataInputStream(socket
						.getInputStream());
				DataOutputStream out = new DataOutputStream(socket
						.getOutputStream());
	            out.writeUTF("permissoes");
				out.writeUTF("listarPermissoesUsuario");
				out.writeInt(usu_id);
				out.flush();
				result = in.readUTF().split(",");
				ArrayList<Permissoes> listaPermissoesUsuario = new ArrayList<Permissoes>();
				
				for (int i = 0; i < result.length; i=i+2) {				
					listaPermissoesUsuario.add(new Permissoes(
							Integer.parseInt(result[i]),
							result[i+1]));
				}					
				
				return listaPermissoesUsuario;
			
			} catch (Exception e) {
				e.printStackTrace();
					// mostra os erros no log
				e.getMessage();
				return null;
			}	
		}

		public ArrayList<String> getPermissoes(int usu_id){
			
			try {
				Socket socket = new Socket("10.0.2.2", 7777);
				DataInputStream in = new DataInputStream(socket
						.getInputStream());
				DataOutputStream out = new DataOutputStream(socket
						.getOutputStream());
	            out.writeUTF("permissoes");
				out.writeUTF("listarPermissoes");
				out.writeInt(usu_id);
				out.flush();
				result = in.readUTF().split(",");
				ArrayList<String> listaPermissoes = new ArrayList<String>();
				
				for (int i = 0; i < result.length; i++) {				
					listaPermissoes.add(new String(
							result[i]
									));
				}					
				//Permissoes permissoes=Permissoes.getInstance();
				//permissoes.setListaPermissoes(listaPermissoes);
				out.close();
				in.close();
				socket.close();
				
				//return permissoes.getListaPermissoes();
			      return listaPermissoes;
			} catch (Exception e) {
				e.printStackTrace();
					// mostra os erros no log
				e.getMessage();
				return null;
			}	
		}

}
